package com.cskaoyan.service;

import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.data.WxBaseData;
import com.cskaoyan.bean.param.TopicParam;
import com.cskaoyan.bean.param.WxBaseParam;
import com.cskaoyan.bean.pojo.MallTopic;
import com.cskaoyan.bean.pojo.MallTopicExample;
import com.cskaoyan.bean.wxvo.WxTopicDetailVO;
import com.cskaoyan.bean.wxvo.WxTopicListVO;
import com.cskaoyan.mapper.MallTopicMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Transactional(rollbackFor = Exception.class)
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MallTopicMapper mallTopicMapper;
    @Override
    public BaseData<MallTopic> queryList(TopicParam param) {
        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallTopicExample mallTopicExample = new MallTopicExample();
        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();
        if(param.getTitle()!=null){
            criteria.andTitleLike("%"+param.getTitle()+"%");
        }
        if(param.getSubtitle()!=null){
            criteria.andSubtitleLike("%"+param.getSubtitle()+"%");
        }
//        只显示未被删除的
        criteria.andDeletedEqualTo(false);
        mallTopicExample.setOrderByClause(param.getSort()+" "+param.getOrder());
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(mallTopicExample);
        long total = mallTopicMapper.countByExample(mallTopicExample);
        return BaseData.ok(mallTopics,total);
    }

    @Override
    public int updateTopic(MallTopic mallTopic) {
        MallTopicExample mallTopicExample = new MallTopicExample();
        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();
        criteria.andIdEqualTo(mallTopic.getId());
        int affectedRows = mallTopicMapper.updateByExample(mallTopic, mallTopicExample);
        if(affectedRows>0){
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteTopic(MallTopic mallTopic) {
        MallTopicExample mallTopicExample = new MallTopicExample();
        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();
        criteria.andIdEqualTo(mallTopic.getId());
        MallTopic mallTopic1 = new MallTopic();
        mallTopic1.setDeleted(true);
        int affectedRows = mallTopicMapper.updateByExampleSelective(mallTopic1, mallTopicExample);
        if(affectedRows>0){
            return 200;
        }
        return 404;
    }

    @Override
    public MallTopic createTopic(MallTopic mallTopic) {
        int affectedRows = mallTopicMapper.insertSelective(mallTopic);
        if(affectedRows>0){
            Date date = new Date();
            mallTopic.setAddTime(date);
            mallTopic.setUpdateTime(date);
            return mallTopic;
        }
        return null;
    }

    @Override
    public WxBaseData<WxTopicListVO> queryTopicList(WxBaseParam wxBaseParam) {
        PageHelper.startPage(wxBaseParam.getPage(),wxBaseParam.getSize());
        MallTopicExample mallTopicExample = new MallTopicExample();
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(mallTopicExample);
        long count = mallTopicMapper.countByExample(mallTopicExample);
//        返回的字段只有六个 而数据库里面查出的字段太多了
        List<WxTopicListVO> mallTopics1 =new ArrayList<>();
        for (MallTopic mallTopic : mallTopics) {
            WxTopicListVO wxTopicListVO = new WxTopicListVO();
            wxTopicListVO.setId(mallTopic.getId());
            wxTopicListVO.setPicUrl(mallTopic.getPicUrl());
            wxTopicListVO.setPrice(mallTopic.getPrice());
            wxTopicListVO.setReadCount(mallTopic.getReadCount());
            wxTopicListVO.setSubtitle(mallTopic.getSubtitle());
            wxTopicListVO.setTitle(mallTopic.getTitle());
            mallTopics1.add(wxTopicListVO);
        }

        return WxBaseData.ok(mallTopics1,count);

    }

    @Override
    public WxTopicDetailVO queryTopicDetail(Integer id) {
        MallTopic mallTopic = mallTopicMapper.selectByPrimaryKey(id);
        String[] goods = mallTopic.getGoods();
        WxTopicDetailVO wxTopicDetailVO = new WxTopicDetailVO();
        wxTopicDetailVO.setGoods(goods);
        wxTopicDetailVO.setTopic(mallTopic);
        return wxTopicDetailVO;
    }

    @Override
    public List<MallTopic> queryTopicRelated() {
        MallTopicExample mallTopicExample = new MallTopicExample();
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(mallTopicExample);
        List<MallTopic> mallTopics1 = new ArrayList<>();
//        拿到集合的长度
        for(int i = 0 ; i<mallTopics.size(); i++){
            mallTopics1.add(mallTopics.get(i));
            if(i==3) break;
        }
        return mallTopics1;
    }

}
